import {AfterContentInit, Component, Input,Output, OnDestroy, ViewEncapsulation,EventEmitter} from "@angular/core";
import {Location} from "@angular/common";
import {screenHelper} from "../../screen.helper";
import {ActivatedRoute, ActivatedRouteSnapshot, NavigationEnd, Router} from "@angular/router";
import {BarPageConfig,Navigator} from "../../domains/domains";

@Component({
    selector:"bar-page",
    templateUrl:"./bar.page.component.html",
    styleUrls:["./bar.page.component.css"]
})
export class BarPageComponent implements OnDestroy{
    titleSubscription:any;
    routerEventSubscriptions:any;
    isShowActionBar:boolean;
    isShowNavigationBar:boolean;

    ngOnDestroy(){
        if(this.titleSubscription) this.titleSubscription.unsubscribe();
        if(this.routerEventSubscriptions) this.routerEventSubscriptions.unsubscribe();
    }
    constructor(private location:Location,
                public pageConfig:BarPageConfig,
                private arouter:ActivatedRoute,
                private router:Router){
        this.showBack=!!history.length;
        this.routerEventSubscriptions=this.router.events.subscribe(evt=>{
            if(evt instanceof NavigationEnd){
                let current:ActivatedRoute=this.arouter;
                while(current.children.length){
                    current=current.children[0];
                }
                if(this.titleSubscription) this.titleSubscription.unsubscribe();
                this.titleSubscription=current.data.subscribe((data:any)=>{
                    if(data && data.title){
                        this.title=data.title;
                    }
                });
                this.isShowActionBar=this.pageConfig.showActionBar(router.url);
                this.isShowNavigationBar=this.pageConfig.showNavigatorBar(router.url);
                this.contentHeight=screenHelper.screenHeightInRem-(this.isShowActionBar?2.5:0);
                this.contentHeight=this.contentHeight-(this.isShowNavigationBar?3:0);
            }
        })

    }
    @Input() showBack:boolean;
    @Input() menus:any=[];
    @Output() emitMenus:EventEmitter<any>=new EventEmitter<any>();
    title:string;

    contentHeight:number=60;
    goBack(){
        this.location.back();
    }
    showMenus(){
        this.emitMenus.emit(this.menus);
    }
}
